import {apiCreateGoods, apiGetGoodsInfo, apiShopTagInfo} from "../../request/api";

export default {
  data() {
    return {
      // 新增商品start
      isShowAddGoods: false,
      goodsInfo: {
        shopId: "",
        goodsUrl: "",
        productId: "",
        shopType: "",
        goodsTitle: "",
        shopName: "",
        showMoney: "",
        goodsImg: "",
        goodsAddress: "",
        token: window.localStorage.getItem('token')
      },
      goodsRules: {
        shopId: [{required: true, message: "请选择店铺", trigger: "blur"}],
        goodsUrl: [{required: true, message: "请输入商品链接", trigger: "blur"}],
        productId: [{required: true, message: "请输入商品id", trigger: "blur"}],
        shopType: [{required: true, message: "请选择店铺种类", trigger: "blur"}],
        goodsTitle: [{required: true, message: "请输入商品标题", trigger: "blur"}],
        shopName: [{required: true, message: "请选择店铺名称", trigger: "blur"}],
        showMoney: [{required: true, message: "请输入拍下单价", trigger: "blur"}],
        goodsImg: [{required: true, message: "请上传商品图片"}]
      },
      shopNameList: [],
      platformType: {
        1: "淘宝",
        2: "天猫",
        3: "京东",
        4: "1688",
        13: "唯品会",
        15: "拼多多"
      },
      // 新增商品end
    }
  },
  created() {
    // 新增商品 start
    this.getShopTagInfo()
    // 新增商品 end
  },
  methods: {
    // #新增商品start
    // 新增商品
    addGoods() {
      this.$refs["goodsInfo"].validate(valid => {
        if (valid) {
          apiCreateGoods(this.goodsInfo).then(res => {
            if (res.code == 1) {
              this.$message.success(res.msg)
              this.$refs["goodsInfo"].resetFields()
              this.isShowAddGoods = false
            } else {// -1
              this.$message.error(res.msg)
            }
          }).catch(res => {
            console.log(res)
          })
        } else {
          this.$message.error("创建的计划信息不完善，请先填写");
          return false
        }
      })
    },
    // 取消新增商品
    cancelAddGoods() {
      this.isShowAddGoods = false
    },
    dataFilter(val) {
      this.goodsInfo.shopId = val;
      if (val) {
        this.shopNameList = this.shopNameList.filter(item => {
          if (
            !!~item.name.indexOf(val) ||
            !!~item.name.toUpperCase().indexOf(val.toUpperCase())
          ) {
            return true;
          }
        });
      }
    },
    // 获取拼接的店铺名称
    getShopTagInfo() {
      apiShopTagInfo({
        token: this.userToken
      })
        .then(res => {
          if (res.code == 1) {
            //店铺列表
            let _this = this
            this.shopNameList = res.msg.shop_list.map((item, i) => {
              return {
                name: `${_this.platformType[item.type]}-${item.name}`,
                type: item.type,
                id: item.id
              }
            })
          }
        })
        .catch(res => {
        });
    },
    grabGoodsInfo() {
      this.$loading.show("抓取中");
      if (this.goodsInfo.goodsUrl != "") {
        if (
          this.goodsInfo.goodsUrl.indexOf("detail.tmall.com") != -1 ||
          this.goodsInfo.goodsUrl.indexOf("item.taobao.com") != -1 ||
          this.goodsInfo.goodsUrl.indexOf("item.jd.com") != -1 ||
          this.goodsInfo.goodsUrl.indexOf("item.m.jd.com") != -1 ||
          this.goodsInfo.goodsUrl.indexOf("yangkeduo.com") != -1 ||
          this.goodsInfo.goodsUrl.indexOf("detail.1688.com") != -1 ||
          this.goodsInfo.goodsUrl.indexOf("pinduoduo.com") != -1
        ) {
          this.getProductId();
          apiGetGoodsInfo({
            goodsUrl: escape(this.goodsInfo.goodsUrl),
            token: this.userToken
          })
            .then(res => {
              this.$loading.hide();
              if (res.code == 1) {
                this.goodsInfo.goodsTitle = res.msg.name;
                this.goodsInfo.goodsSeller = res.msg.seller_name;
                this.goodsInfo.shopName = res.msg.shop_name;
                this.goodsInfo.coName = res.msg.co_name;
                this.goodsInfo.shopType = res.msg.shop_type;
                this.goodsInfo.goodsImg = res.msg.goods_img;
                this.goodsInfo.productId = res.msg.product_id;
                return this.$message.success("抓取商品信息成功");
              } else {
                this.$message.error(res.msg);
              }
            })
            .catch(res => {
              this.$loading.hide();
              this.$message.error(res.msg);
            });
        } else if (
          this.goodsInfo.goodsUrl.indexOf("m.vip.com") != -1 ||
          this.goodsInfo.goodsUrl.indexOf("detail.vip.com") != -1
        ) {
          this.$loading.hide();
          this.goodsInfo.goodsTitle = "";
          this.goodsInfo.goodsSeller = "";
          this.goodsInfo.shopName = "";
          this.goodsInfo.shopType = "";
        } else {
          this.$loading.hide();
          return this.$message.error("请输入正确的商品链接再抓取");
        }
      } else {
        this.$loading.hide();
        return this.$message.error("请输入商品链接再抓取");
      }
    },
    getProductId() {
      if (
        this.goodsInfo.goodsUrl.indexOf("item.jd.com") != -1 ||
        this.goodsInfo.goodsUrl.indexOf("item.m.jd.com") != -1
      ) {
        let urlArr = this.goodsInfo.goodsUrl.split(".html")[0];
        let urlArr1 = urlArr.split("/");
        this.goodsInfo.productId = urlArr1[urlArr1.length - 1];
      }
      if (
        this.goodsInfo.goodsUrl.indexOf("detail.tmall.com") != -1 ||
        this.goodsInfo.goodsUrl.indexOf("item.taobao.com") != -1
      ) {
        this.goodsInfo.productId = this.getRequestParam(
          this.goodsInfo.goodsUrl,
          "id"
        );
      }
      if (this.goodsInfo.goodsUrl.indexOf("yangkeduo.com") != -1) {
        this.goodsInfo.productId = this.getRequestParam(
          this.goodsInfo.goodsUrl,
          "goods_id"
        );
      }
      if (this.goodsInfo.goodsUrl.indexOf("detail.1688.com") != -1) {
        let urlArr = this.goodsInfo.goodsUrl.split(".html")[0];
        let urlArr1 = urlArr.split("/");
        this.goodsInfo.productId = urlArr1[urlArr1.length - 1];
      }
      if (this.goodsInfo.goodsUrl.indexOf("pinduoduo.com") != -1) {
        this.goodsInfo.productId = this.getRequestParam(
          this.goodsInfo.goodsUrl,
          "gid"
        );
      }
    },
    getRequestParam(url, name) {
      var reg = new RegExp("(^|&|\\?)" + name + "=([^&]*)(&|$)", "i");
      var r = url.substr(1).match(reg);
      if (r != null) return unescape(r[2]);
      return null;
    },
    handleSuccessAddGoodsImg(res, file) {
      this.goodsInfo.goodsImg = res.msg;
      this.$message.success("上传成功");
    },
    beforeUploadAddGoodsImg(file) {
      const isLt2M = file.size / 1024 / 1024 < 10;
      if (!isLt2M) {
        this.$message.error("上传商品图片大小不能超过 10MB!");
      }
      return isLt2M;
    },
    // #新增商品end
  }
}
